package com.campus.util;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 获取当前http请求的用户ID
 * @author 荧moon
 * @date 2024/12/26
 */
public class GetUserIdUtil {

    public static Integer getUserId(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() instanceof Integer) {
            return (Integer) authentication.getPrincipal();
        }
        return Integer.valueOf(-1);
    }

}
